<%@ page contentType="text/html;charset=UTF-8" %>
  <!DOCTYPE html>
  <html>

  <head>
    <title>管理员注册测试</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  </head>

  <body>
    <h1>管理员注册测试页面</h1>

    <div>
      <h3>测试控制器连接</h3>
      <button id="testBtn">测试控制器</button>
      <div id="testResult"></div>
    </div>

    <hr>

    <div>
      <h3>注册表单</h3>
      <form id="registerForm">
        <p>
          <label>用户名:</label>
          <input type="text" id="admin_name" value="testadmin">
        </p>
        <p>
          <label>昵称:</label>
          <input type="text" id="admin_nickname" value="测试管理员">
        </p>
        <p>
          <label>密码:</label>
          <input type="password" id="admin_password" value="123456">
        </p>
        <p>
          <button type="button" id="registerBtn">注册</button>
        </p>
      </form>
      <div id="registerResult"></div>
    </div>

    <script>
      $(document).ready(function () {
        console.log("测试页面加载完成");

        // 测试控制器连接
        $('#testBtn').click(function () {
          console.log("测试控制器连接");
          $.ajax({
            url: '${pageContext.request.contextPath}/admin/register/test',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
              console.log('测试成功:', data);
              $('#testResult').html('<p style="color: green;">控制器连接成功: ' + data.message + '</p>');
            },
            error: function (xhr, status, error) {
              console.log('测试失败:', xhr, status, error);
              $('#testResult').html('<p style="color: red;">控制器连接失败: ' + error + '</p>');
            }
          });
        });

        // 注册测试
        $('#registerBtn').click(function () {
          console.log("测试注册功能");

          var admin_name = $('#admin_name').val();
          var admin_nickname = $('#admin_nickname').val();
          var admin_password = $('#admin_password').val();

          console.log("注册数据:", {
            admin_name: admin_name,
            admin_nickname: admin_nickname,
            admin_password: admin_password
          });

          $.ajax({
            url: '${pageContext.request.contextPath}/admin/register',
            type: 'POST',
            data: {
              admin_name: admin_name,
              admin_nickname: admin_nickname,
              admin_password: admin_password
            },
            dataType: 'json',
            success: function (data) {
              console.log('注册成功:', data);
              if (data.success) {
                $('#registerResult').html('<p style="color: green;">注册成功: ' + data.message + '</p>');
              } else {
                $('#registerResult').html('<p style="color: orange;">注册失败: ' + data.message + '</p>');
              }
            },
            error: function (xhr, status, error) {
              console.log('注册失败:', xhr, status, error);
              $('#registerResult').html('<p style="color: red;">注册失败: ' + error + '<br>响应: ' + xhr.responseText + '</p>');
            }
          });
        });
      });
    </script>
  </body>

  </html>